<?php
namespace Home\Controller;
use Think\Log;

/**
 * 会员中心
 * User: linkwhy
 * Date: 2015/10/26
 * Time: 19:20
 */
class MemberController extends HomeController{

    private $wechar_id = 0;
    private $mode = '';
    private $area = '';
    private $ress = '';

    public function _initialize(){
        $this->wechar_id = $_SESSION['wechat_id'];
        $this->mode = M('clients','caitou_','DB_CONFIG1');
        $this->area = M('areas');
        $this->ress = M('clients_address','caitou_','DB_CONFIG1');
    }

    public function index(){
        $row = $this->mode->field('id,nickname,headimgurl,phone')->where(array('id' => $this->wechar_id, 'subscribe_status' => 1))->find();
              //待付款；待收货；退货
        $order = M('order');
        $row['notpay'] = $order->where(array('client_id' => $this->wechar_id, 'order_status' => 0))->count();   //待付款；
        $row['notgoods'] = $order->where("client_id = {$this->wechar_id} and order_status in(2,3)")->count();   //待收货；
        $row['return'] = M('return_goods')->where(array('client_id' => $this->wechar_id, 'return_status' => 2))->count();   //退货；
        $this->assign('row', $row);
        $this->display();
    }

    public function getOrderCount($status){
        return M('order')->where(array('client_id' => $this->wechar_id, 'order_status' => $status))->count();
    }

    /**
     * 账户管理
     */
    public function userManager(){
        $count = $this->ress->where(array('cid' => $this->wechar_id, 'is_del' => 0))->count();
        if($count > 0){
            $list = $this->ress->field('id,name,phone,address,default,pro_id,city_id,area_id')->where(array('cid' => $this->wechar_id, 'is_del' => 0))->select();
            $data['name'] = '收货地址';
            $data['order_type'] =  I('get.order_type', 'no');
            $this->assign('data', $data);
            $this->assign('list', $list);
            $this->display('index_address');
        }else{
            $this->redirect("Member/add_address", array('order_type' => I('get.order_type', 'no')));
        }

    }

    /**
     * 收货地址
     */
    public function index_address(){
        $list = $this->ress->field('id,name,phone,address,default')->where(array('cid' => $this->wechar_id, 'is_del' => 0))->select();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 添加收货地址
     */
    public function add_address(){
        $data['name'] = "添加收货地址";
        $count = $this->ress->where(array('cid' => $_SESSION['userid'], 'default' => 1, 'type' => 0))->count();
        $data['order_type'] = I('get.order_type');
        $data['count'] = $count;
        $this->assign('list', $this->province());
        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 编辑收货地址
     */
    public function edit_address(){
        $row = $this->ress->field(true)->where(array('id' => I('get.id')))->find();
        $data['name'] = "编辑收货地址";
        $data['order_type'] = I('get.order_type');
        $this->assign('row', $row);
        $this->assign('data', $data);
        $this->assign('list', $this->province());
        $this->assign('list_city', $this->area->field('area_id,area_name')->where(array('parent_id' => $row['pro_id'], 'area_type' => 2))->select());
        $this->assign('list_area', $this->area->field('area_id,area_name')->where(array('parent_id' => $row['city_id'], 'area_type' => 3))->select());
        $this->display('add_address');
    }

    public function del_address(){
        if( $this->ress->where(array('id' => I('get.id')))->setField('is_del', 1) ){
            $this->ajaxReturn(array('status' => true));
        }else{
            $this->ajaxReturn(array('status' => false));
        }
    }

    public function save_address(){
        $id = I('post.id');
        $order_type = I('post.order_type');
        $sdata = array(
            'cid' => $this->wechar_id,
            'name' => I('post.name'),
            'phone' => I('post.phone'),
            'address' => I('post.address'),
            'code' => I('post.code'),
            'pro_id' => I('post.pro_id'),
            'city_id' => I('post.city_id'),
            'area_id' => I('post.area_id'),
            'default' => I('post.default_address')
        );
        if(I('post.default_address') == 1){
            $this->ress->where(array('cid' => $this->wechar_id))->setField('default', 0);
        }
        $this->ress->create($sdata);
        $rs = false;
        if($id > 0){
            if($this->ress->where(array('id' => $id))->save()){
                $rs = true;
            }else{
                $rs = false;
            }
        }else{
            //保存
            if($this->ress->add()){
                $rs = true;
            }else{
                $rs = false;
            }
        }

        if($rs){
            if($order_type == 'yes'){
                $this->ajaxReturn(array('status' => true, 'url' => U('Order/order_index')));
            }else{
                $this->ajaxReturn(array('status' => true, 'url' => U('index_address')));
            }
        }
    }

    public function updateDefault(){
        $id = I('get.id');
        $order_type = I('get.order_type');
        $this->ress->where(array('cid' => $this->wechar_id))->setField('default', 0);
        if( $this->ress->where(array('id' => $id))->setField('default', 1) ){
            if($order_type == 'yes'){
                $this->ajaxReturn(array('status' => true, 'url' => U('Order/order_index')));
            }else{
                $this->ajaxReturn(array('status' => true, 'url' => U('index_address')));
            }
        }
    }

    /**
     * 省份
     */
    public function province(){
        return $this->area->field('area_id,area_name')->where(array('parent_id' => 1, 'area_type' => 1))->select();
    }

    /**
     * 城市
     */
    public function city(){
        $list = $this->area->field('area_id,area_name')->where(array('parent_id' => I('get.pro_id'), 'area_type' => 2))->select();
        if($list){
            $this->ajaxReturn(array('status' => true, 'list' => $list));
        }else{
            $this->ajaxReturn(array('status' => false));
        }
    }

    /**
     * 区域
     */
    public function city_area(){
        $list = $this->area->field('area_id,area_name')->where(array('parent_id' => I('get.city_id'), 'area_type' => 3))->select();
        if($list){
            $this->ajaxReturn(array('status' => true, 'list' => $list));
        }else{
            $this->ajaxReturn(array('status' => false));
        }
    }

    /**
     * 用户收藏
     */
    public function userCollect(){
        $list = M('goods')->alias('g')->field('g.id,name,picture,price,shop_price,zhekou')->join('__COLLECT__ c ON c.goods_id = g.id')->where(array('cid' => $this->wechar_id))->select();
        $data['name'] = '我的鞋柜';
        $this->assign('list', $list);
        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 关于点粉圈
     */
    public function aboutinfo(){
        $data = M('info')->field(true)->order('id desc')->limit(1)->find();
        $data['name'] = '关于点粉圈';
        $this->assign('data', $data);
        $this->display();
    }

    public function bank_index(){
        $row =  $this->mode->field('id,open_name,bank_account,open_bank_name')->where(array('uid' => $this->wechar_id))->find();
        $list_bank = M('bank')->field(true)->select();
        $data['name'] = '绑定银行卡';
        $row['prevurl'] = I('get.prevurl');
//        $row['order_id'] = I('get.order_id');
        $this->assign('row', $row);
        $this->assign('data', $data);
        $this->assign('list_bank', $list_bank);
        $this->display();
    }

    public function update_bank(){
        $prevurl = I('post.prevurl');
        $postData = array(
            'open_name' => I('post.open_name'),
            'bank_account' => I('post.bank_account'),
            'open_bank_name' => I('post.open_bank_name')
        );
        $rs = $this->mode->where("uid={$this->wechar_id}")->save($postData);

        if( $rs ){
            if($prevurl == 'yes'){
                //$this->ajaxReturn(array('status' => true, 'url' => U('ReturnGoods/index', array('order_id' => I('post.order_id')))));
                $this->ajaxReturn(array('status' => true, 'url' => U('Order/index')));
            }else{
                $this->ajaxReturn(array('status' => true, 'url' => U('index')));
            }
        }else{
            $this->ajaxReturn(array('status' => false));
        }
    }


}